﻿
Partial Public Class [property]
    Implements iproperty(Of bytes)

    Public Function get_data(ByVal data As pointer(Of bytes)) As eventComb _
                            Implements iproperty(Of bytes).get_data
        Dim pb As pointer(Of Boolean) = Nothing
        Dim rec As eventComb = Nothing
        Return New eventComb(Function() As Boolean
                                 rec = accessor().read(key, data)
                                 Return waitfor(rec, read_timeout_ms) AndAlso
                                        goto_next()
                             End Function,
                             Function() As Boolean
                                 If rec.end_result() Then
                                     Return goto_end()
                                 Else
                                     pb = New pointer(Of Boolean)()
                                     Return waitfor(io(Function() accessor().exist(key, pb),
                                                       read_timeout_ms,
                                                       read_retry_wait_ms,
                                                       "exist")) AndAlso
                                            goto_next()
                                 End If
                             End Function,
                             Function() As Boolean
                                 Return +pb AndAlso
                                        waitfor(read_retry_wait_ms) AndAlso
                                        goto_begin()
                             End Function)
    End Function

    Public Function set_data(ByVal value As bytes) As eventComb _
                            Implements iproperty(Of bytes).set_data
        Return io(Function() accessor().write(key, value), write_timeout_ms, write_retry_wait_ms, "write")
    End Function

    Public Function destroy() As eventComb _
                           Implements iproperty(Of bytes).destroy
        Return io(Function() accessor().erase(key), write_timeout_ms, write_retry_wait_ms, "destroy")
    End Function

    Public Function get_set_data(ByVal d As _do(Of bytes, Boolean)) As eventComb _
                                Implements iproperty(Of bytes).get_set_data
        Return get_set_data(get_set_data_proxy(d))
    End Function

    Public Function get_set_data(ByVal d As get_set_data_d(Of bytes)) As eventComb _
                                Implements iproperty(Of bytes).get_set_data
        assert(Not d Is Nothing)
        Dim gec As eventComb = Nothing
        Dim dec As eventComb = Nothing
        Dim sec As eventComb = Nothing
        Dim p As pointer(Of bytes) = Nothing
        Return New eventComb(Function() As Boolean
                                 p = New pointer(Of bytes)()
                                 gec = get_data(p)
                                 Return waitfor(gec) AndAlso
                                        goto_next()
                             End Function,
                             Function() As Boolean
                                 assert(gec.end_result())
                                 assert(Not +p Is Nothing)
                                 dec = d(p)
                                 assert(Not dec Is Nothing)
                                 Return waitfor(dec) AndAlso
                                        goto_next()
                             End Function,
                             Function() As Boolean
                                 If dec.end_result() AndAlso Not +p Is Nothing Then
                                     sec = set_data(+p)
                                     Return waitfor(sec) AndAlso
                                            goto_next()
                                 Else
                                     Return False
                                 End If
                             End Function,
                             Function() As Boolean
                                 assert(sec.end_result())
                                 Return goto_end()
                             End Function)
    End Function
End Class
